/*
 * @Copyright: EasyCompany 2021
 * @Author: asterisk
 * @Date: 2021-03-18 19:56:26
 * @Description: 
 */
import 'package:date_time_format/date_time_format.dart';

import '../../easy_log.dart';

class EasyLogInfoFormatter implements EasyLogFormatter {
  final String formatPattern = 'Y.m.d';
  EasyLoggerConfig _config; //ref;
  EasyLogInfoFormatter(this._config);
  @override
  dynamic format(dynamic message) {
    String version = _formatVersion(message);
    String date = _formatDate(message);
    String name = _formatName();
    String type = _formatType(message);
    String language = 'APP LANGUAGE: Dart';
    String logName = _formatLogName(message);
    String gitTag = _formatGitTag();
    var list = [];
    var ret = <String>[];
    ret.add('#' * 80);
    if (EasyLogInfoType.head == message.type) {
      list
        ..add(type)
        ..add(name)
        ..add(version)
        ..add(language)
        ..add(date)
        ..add(gitTag)
        ..add(logName);
    } else {
      list..add(type)..add(logName);
    }

    for (var item in list) {
      ret.add('#' * 20 +
          ' ' * 4 +
          item +
          ' ' * (60 - 20 - 4 - item.length) +
          '#' * (20));
    }

    ret.add('#' * 80);
    return ret;
  }

  String _formatDate(EasyLogInfo message) {
    final dtl = DateTime.now().toLocal();
    return 'APP DATE: ' + dtl.format(formatPattern);
  }

  String _formatVersion(EasyLogInfo message) {
    String ret = '';
    for (var item in message.version) {
      ret += '.' + item.toString();
    }
    if (ret.isNotEmpty) {
      ret = ret.substring(1);
    }
    ret = 'APP VERSION: ' + ret;
    return ret;
  }

  String _formatType(EasyLogInfo message) {
    if (EasyLogInfoType.head == message.type) {
      return 'LOG RECORD START';
    } else if (EasyLogInfoType.foot == message.type) {
      return 'LOG RECORD END';
    } else {
      return '';
    }
  }

  String _formatName() {
    return 'APP NAME: ${EasyLogConst.appDefaultCompany.toUpperCase()} ${EasyLoggerFactory.appName.toUpperCase()}';
  }

  String _formatLogName(EasyLogInfo message) {
    return 'LOG NAME: ${_config.logName}';
  }

  String _formatGitTag() {
    return 'GIT TAG: ${EasyLoggerFactory.gitTag}';
  }
}
